
package com.tools.cleanmaster.ui.view.trash;

import android.content.Context;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.item.TrashItem;
import com.tools.cleanmaster.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class TrashGroup implements TrashViewItem {
    protected final TrashGroup mParent;

    protected List<TrashViewItem> mChildItems = new ArrayList<TrashViewItem>();

    protected long mScannedTotalSize = 0l;

    protected final Context mContext;

    boolean mIsChecked;

    boolean mIsCheckedDefault = true;

    boolean mIsHalfChecked = false;

    TrashGroup(TrashGroup parent) {
        mContext = CleanApplication.getInstance();
        mParent = parent;
    }

    /**
     * 是否显示
     * 
     * @return mScannedTotalSize大于０则显示
     */
    public boolean shouldBeDisplayed() {
        return mScannedTotalSize > 0;
    }

    /**
     * 填数据
     * 
     * @param trashes
     */
    abstract public void fillTrashes(Map<TrashType, List<TrashItem>> trashes);

    /**
     * 返回子数据
     * @return
     */
    public final List<TrashViewItem> getChildItems() {
        return mChildItems;
    }

    /**
     * 按子项计算增加总size
     * 
     * @param item
     */
    protected void addChildItem(TrashViewItem item) {
        mChildItems.add(item);
        mScannedTotalSize += item.getTotalSize();
    }

    @Override
    public long getSelectedSize() {
        if (isChecked()) {
            return mScannedTotalSize;
        } else if (!isHalfChecked()) {// 全不选
            return 0;
        } else {// 非全选
            long size = 0l;
            for (TrashViewItem childItem : mChildItems) {
                size += childItem.getSelectedSize();
            }
            return size;
        }
    }

    @Override
    public int compareTo(TrashViewItem item) {
        if (item instanceof TrashGroup) {
            long comparedSize = ((TrashGroup) item).mScannedTotalSize;
            if (mScannedTotalSize < comparedSize) {
                return 1;
            } else if (mScannedTotalSize > comparedSize) {
                return -1;
            } else {
                return 0;
            }
        } else {
            return -1;
        }
    }

    @Override
    public String getStatus() {
        return StringUtils.getSizeString(mScannedTotalSize);
    }

    @Override
    public boolean isChecked() {
        return mIsChecked;
    }

    @Override
    public boolean isHalfChecked() {
        return mIsHalfChecked;
    }

    @Override
    public void onCheckedChanged(boolean isChecked, boolean callParent) {
        mIsChecked = isChecked;
        mIsHalfChecked = false;
        for (TrashViewItem childItem : mChildItems) {
            childItem.onCheckedChanged(isChecked, false);
        }
        if (mParent != null && callParent) {
            mParent.onChildCheckedChanged();
        }
    }

    public void onChildCheckedChanged() {
        setCheckStateByChild();
        if (mParent != null) {
            mParent.onChildCheckedChanged();
        }
    }

    protected void setCheckStateByChild() {
        boolean isAllSelect = true;
        boolean hasOneSelect = false;
        for (TrashViewItem childItem : mChildItems) {
            if (childItem.isChecked() || childItem.isHalfChecked()) {
                hasOneSelect = true;
            }
            // 只要孩子中有一个是没有选或半选的，则mIsChecked为false
            if (!childItem.isChecked() || childItem.isHalfChecked()) {
                isAllSelect = false;
                if (hasOneSelect) {// 如此此时hasOneSelect，则break
                    break;
                }
            }
        }
        mIsChecked = isAllSelect;
        mIsHalfChecked = hasOneSelect && !isAllSelect;// 非全选且至少有一个选
    }

    @Override
    public long getTotalSize() {
        return mScannedTotalSize;
    }

    /**
     * @return 所有选中的项
     */
    public ArrayList<TrashItem> getSelectedTrahItems() {
        ArrayList<TrashItem> itemsSelect = new ArrayList<TrashItem>();
        for (TrashViewItem item : mChildItems) {
            if (item instanceof TrashGroup) {
                if (item.isChecked() || item.isHalfChecked()) {
                    itemsSelect.addAll(((TrashGroup) item).getSelectedTrahItems());
                }
            } else if (item instanceof TrashViewItemSingle) {
                if (item.isChecked()) {
                    itemsSelect.add(((TrashViewItemSingle) item).mTrashItem);
                }
            }
        }
        return itemsSelect;
    }
}
